import nextpy as xt
from nextpy.frontend.page import get_decorated_pages

class State(xt.State):
    
    sidebar_header : dict[str, list[str, dict]] 

    sidebar_home: str

    def sidebar_list(self):
        self.sidebar_header = {}
        pages = get_decorated_pages()
        pages = sorted(pages, key=lambda x: int(x['description']))
        for page in pages:
            title = page['title']
            if page['route']!='/':
                item_list = self.sidebar_header.get(title, [])
                route = page['route']
                while route[0].isdigit():
                    route = route[1:]
                item_list.append(route)
                self.sidebar_header[title] = item_list
            else:
                self.sidebar_home = page['route']
        self.sidebar_header = dict(sorted(self.sidebar_header.items()))
        yield
